
package cn.xabad.commons.io.input;

import java.io.IOException;
import java.io.InputStream;


public class DemuxInputStream
    extends InputStream
{
    private final InheritableThreadLocal<InputStream> m_streams = new InheritableThreadLocal<InputStream>();

    
    public InputStream bindStream( InputStream input )
    {
        InputStream oldValue = m_streams.get();
        m_streams.set( input );
        return oldValue;
    }

    
    @Override
    public void close()
        throws IOException
    {
        InputStream input = m_streams.get();
        if( null != input )
        {
            input.close();
        }
    }

    
    @Override
    public int read()
        throws IOException
    {
        InputStream input = m_streams.get();
        if( null != input )
        {
            return input.read();
        }
        else
        {
            return -1;
        }
    }
}
